package com.banggood.upms.aspect;

import com.banggood.silk.web.api.exception.SkbException;
import com.banggood.upms.bean.exception.ExceptionEnum;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 新增，修改，删除这三个service方法判断是否成功，不能则抛出异常。
 * 抛出异常之后，无法正常封装返回参数，暂时弃用！
 */
//@Aspect
@Order(2)
@Component
public class CUDExceptionAspect {

    protected final Log logger = LogFactory.getLog(this.getClass());

    @Pointcut("execution(public * com.banggood.upms.service.impl.BaseServiceImpl.insert(..))" +
            "|| execution(public * com.banggood.upms.service.impl.BaseServiceImpl.updateByPrimaryKey(..))" +
            "|| execution(public * com.banggood.upms.service.impl.BaseServiceImpl.deleteByPrimaryKey(..))")
    public void handlerCUD(){}

    @Before("handlerCUD()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
    }

    @AfterReturning(returning = "count", pointcut = "handlerCUD()")
    public void doAfterReturning(int count) throws Throwable {
        // 处理完请求
        if(count != 1) {
            throw new SkbException(ExceptionEnum.OPERATION_FAILED.getCode(), ExceptionEnum.OPERATION_FAILED.getMessage());
        }
    }
}

